package com.tonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tonghe.domain.ResponseResult;
import com.tonghe.domain.dto.TagDto;
import com.tonghe.domain.entity.Tag;
import com.tonghe.enums.AppHttpCodeEnum;
import com.tonghe.mapper.TagMapper;
import com.tonghe.service.TagService;
import com.tonghe.utils.BeanCopyUtils;
import com.tonghe.utils.SecurityUtils;
import com.tonghe.vo.PageVo;
import com.tonghe.vo.sys.TagVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 标签(Tag)表服务实现类
 *
 * @author tonghe
 * @since 2022-07-24 23:29:05
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    /** 备注 */
    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagDto tagDto) {

        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(tagDto.getName()), Tag::getName, tagDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagDto.getRemark()), Tag::getRemark, tagDto.getRemark());

        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    /** 获取所有标签 */
    @Override
    public ResponseResult getAllTag() {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId, Tag::getName);
        List<Tag> tagList = list(queryWrapper);
        return ResponseResult.okResult(tagList);
    }

    /** 标签详情 */
    @Override
    public ResponseResult selectOne(long id) {
        Tag byId = getById(id);
        // 根据参数 id 查询对象并判断是否为空
        if (Objects.isNull(byId)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.OBJECT_ISNULL);
        }
        TagVo tagVo = BeanCopyUtils.copyBean(byId, TagVo.class);
        return ResponseResult.okResult(tagVo) ;
    }

    /** 插入新标签 */
    @Override
    public ResponseResult insertTag(TagDto tagDto) {

        if(!StringUtils.hasText(tagDto.getName()) || !StringUtils.hasText(tagDto.getRemark())){
            return ResponseResult.errorResult(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        if(checkTagNameUnique(tagDto.getName(),0L)){
            Tag tag = BeanCopyUtils.copyBean(tagDto, Tag.class);
            int insert = baseMapper.insert(tag);
            return insert > 0 ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NAME_EXITS);
        }

    }

    /** 更新标签 */
    @Override
    public ResponseResult updateTag(Tag tag) {
        // TODO 逻辑删除的也能改？
        // FIXME 我自己修改我自己，也被识别为名称已存在
        if(!checkTagNameUnique(tag.getName(), tag.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NAME_EXITS);
        }
        tag.setUpdateBy(SecurityUtils.getUserId());

        return baseMapper.updateTag(tag) > 0 ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    /** 通过id删除标签 */
    @Override
    public ResponseResult removeTagById(long id) {
        // TODO 删除相关关联
        int bool = baseMapper.deleteById(id);
        return bool > 0 ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public Boolean checkTagNameUnique(String name, Long id) {
        Tag tag = baseMapper.checkTagNameUnique(name, id);
        if( !Objects.isNull(tag) ) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean checkTagExistArticle(Long tagId) {
        // TODO 烂尾 标签和文章目前还没有链接起来
        if(!Objects.isNull(baseMapper.checkTagExistArticle(tagId))) {
            return false;
        } else {
            return true;
        }
    }


}
